#include "afe_cmd.h"
#include <stdio.h>
#include <stdlib.h>

extern afe_dev_t afe1; // 必须声明外部模拟前端设备

int main(int argc, char *argv[])
{
    int ret = 0;
    chx_mode_t ch3_mode = 0xff;
    pga_gain_t ch3_gain = 0xff;
    samp_rate_t samp_rate = 0xff;
    uint8_t fw[5] = {0};

    if (argc == 2)
    {
        afe1.spi->dp_en = atoi(argv[1]);
    }
    // afe1.spi->dp_en = true;

    ret = afe1.spi->open(); // 必须调用打开spi设备
    if (ret < 0)
    {
        return ret;
    }

    // 获取并打印设备采样率
    ret = afe_get_samp_rate(&afe1, &samp_rate);
    if (ret != 0)
    {
        printf("get samp rate error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("afe samprate: 0x%x\n", samp_rate);
    }

    // 获取并打印通道3增益
    ret = afe_get_chx_gain(&afe1, AFE_CH_3, &ch3_gain);
    if (ret != 0)
    {
        printf("get ch3 gain error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("get chx gain success: 0x%x\n", ch3_gain);
    }

    // 获取并打印通道3模式
    ret = afe_get_chx_mode(&afe1, AFE_CH_3, &ch3_mode);
    if (ret != 0)
    {
        printf("get ch3 mode error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("get chx mode success: 0x%x\n", ch3_mode);
    }

    // 设置所有通道增益
    ret = afe_set_chx_gain(&afe1, AFE_CH_ALL, PGA_GAIN_DIV_2);
    if (ret != 0)
    {
        printf("set chx gain error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("set chx gain success!\n");
    }

    // 设置所有通道模式
    ret = afe_set_chx_mode(&afe1, AFE_CH_ALL, CHX_MODE_IEPE);
    if (ret != 0)
    {
        printf("set chx mode error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("set chx mode success!\n");
    }

    // 设置采样率
    ret = afe_set_samp_rate(&afe1, SAMP_100K);
    if (ret != 0)
    {
        printf("set samp rate error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("set samp rate success!\n");
    }

    // 开始采集
    ret = afe_acq_begin(&afe1);
    if (ret != 0)
    {
        printf("set acq start error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("set acq start success!\n");
    }

    // 获取并打印通道3增益
    ch3_gain = 0xff;
    ret = afe_get_chx_gain(&afe1, AFE_CH_3, &ch3_gain);
    if (ret != 0)
    {
        printf("get ch3 gain error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("get chx gain success: 0x%x\n", ch3_gain);
    }

    // 获取并打印通道3模式
    ch3_mode = 0xff;
    ret = afe_get_chx_mode(&afe1, AFE_CH_3, &ch3_mode);
    if (ret != 0)
    {
        printf("get ch3 mode error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("get chx mode success: 0x%x\n", ch3_mode);
    }

    // 获取并打印版本号
    ret = afe_get_fw(&afe1, fw);
    if (ret != 0)
    {
        printf("get afe fw error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        fw[4] = '\0';
        printf("afe board firmware: %s\n", fw);
    }

    // 获取并打印设备采样率
    samp_rate = 0xff;
    ret = afe_get_samp_rate(&afe1, &samp_rate);
    if (ret != 0)
    {
        printf("get samp rate error! error code: %d !\n", ret);
        return ret;
    }
    else
    {
        printf("afe samprate: 0x%x\n", samp_rate);
    }

    afe1.spi->close(); // 必须调用关闭spi设备

    return 0;
}